import React, { useCallback, useImperativeHandle, useRef, useState } from 'react';
import type { AudDialogShowParams } from '@/pages/components/Aud/AudDialog';
import { Popup } from 'devextreme-react';
import { Select } from 'antd';
import { intl } from '@/utils';
import { useModel } from 'umi';
import { initCutOffDate, getMonthList, updateCutOffDate } from './service';


const ReportFinancialToResetMonth: React.FC<any> = React.forwardRef((props: any, ref) => {

  const { Content, AccountDataID } = props;

  const { initialState } = useModel('@@initialState');
  const { dbname } = initialState?.curPart as API.CurPart;
  const { username } = initialState?.curUser as API.CurUser;

  const refsForm = useRef<any>();
  const [Value, setValue] = useState<any>('');
  const diaParams = useRef<AudDialogShowParams & any>();
  const [visible, setVisible] = useState<boolean>(false);
  const [PaperMonth, setPaperMonth] = useState<any[]>([]);
  const [PaperValue, setPaperValue] = useState<string>('');

  const getMonthLists = async () => {
    if (!AccountDataID) return;
    try {
      const res = await getMonthList({
        dbname,
        Data_ID: AccountDataID,
      })
      console.log(AccountDataID, 'AccountDataID--------');

      console.log(res, 'res-----');
      setPaperMonth(res);
    } catch (error) {
      console.log(error);
    }
  };

  // 自定义方法
  useImperativeHandle(ref, () => ({
    show: async (params: AudDialogShowParams, SelectValue: string) => {
      diaParams.current = params;
      setVisible(true);
      setValue(SelectValue);
      getMonthLists();
    },
    close: () => { setVisible(false) }
  }));

  const fetchData = async (byMonth: string) => {
    Content(PaperValue ? PaperValue : Value);
    const res: any = await updateCutOffDate({
      dbname,
      Data_ID: AccountDataID,
      byMonth,
      editUser: username
    });
    if (res.success) {
      diaParams.current?.OkCallback();
    }


    // initCutOffDates();
    // aa();
  };
  // const initCutOffDates = async () => {
  //   const r = await initCutOffDate({
  //     dbname,
  //     Data_ID: AccountDataID,
  //     isForceInit: false
  //   })
  //   console.log(r, 'r-------');
  // }
  // 测试代码 -------------- 
  // const aa = async ()=>{
  //   await adjustingEntry({
  //     dbname,
  //     Data_ID: AccountDataID,
  //     editUser: username,
  //   })
  // }

  const onOk = () => {
    setVisible(false);
    fetchData(PaperValue);

  }

  const onCancel = useCallback(() => {
    setVisible(false)
  }, [])

  // 获取列表的点击项 
  const onSelectionChangeds = useCallback((e: any) => {
    setPaperValue(e);
    setValue(e);
  }, [])

  return (
    <>
      <Popup
        ref={ref}
        visible={visible}
        wrapperAttr={{
          class: "fix-popup-title-width"
        }}
        title='重新设置截止月份'
        width={380}
        height={200}
        {...props}
        onHiding={() => setVisible(false)}
        toolbarItems={[
          {
            widget: 'dxButton',
            toolbar: 'bottom',
            location: 'after',
            options: {
              text: intl('pub.cancel', '取消'),
              onClick: () => onCancel()
            },
          },
          {
            widget: 'dxButton',
            toolbar: 'bottom',
            location: 'after',
            options: {
              text: intl('pub.ok', '确定'),
              type: 'default',
              onClick: () => onOk()
            },
          },
        ]}
      >
        <div
          style={{
            display: 'flex',
            justifyContent: 'space-around',
            alignItems: 'center'
          }}
        >
          <Select
            value={Value}
            style={{ width: '100%' }}
            className='AntdSelect'
            options={PaperMonth?.length ? (PaperMonth || []).map((d: any) => ({
              value: d.YF,
              label: d.YF,
            })) : []}
            onChange={onSelectionChangeds}
          />
        </div>
      </Popup>
    </>
  );
});

export default ReportFinancialToResetMonth;